package com.yhn.shortlink.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.shortlink.common.biz.UserContext;
import com.yhn.shortlink.admin.dto.req.group.GroupSortReqDTO;
import com.yhn.shortlink.admin.dto.req.group.GroupUpdateReqDTO;
import com.yhn.shortlink.admin.dto.resp.group.GroupRespDTO;
import com.yhn.shortlink.common.entity.LinkGroupDO;
import com.yhn.shortlink.common.enums.LinkErrorEnums;
import com.yhn.shortlink.common.exception.ThrowUtils;
import com.yhn.shortlink.admin.mapper.LinkGroupMapper;
import com.yhn.shortlink.admin.service.LinkGroupService;
import com.yhn.shortlink.common.utils.RandomGeneratorUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86177
 * @description 针对表【group】的数据库操作Service实现
 * @createDate 2024-07-23 13:43:21
 */
@Service

public class LinkGroupServiceImpl extends ServiceImpl<LinkGroupMapper, LinkGroupDO>
        implements LinkGroupService {

    @Override
    public boolean addGroup(String groupName) {
        String gid;
        do {
            gid = RandomGeneratorUtils.generateRandomString();
        } while (!hasGid(gid));
        LinkGroupDO groupDO = LinkGroupDO.builder()
                .gid(RandomGeneratorUtils.generateRandomString())
                .name(groupName)
                .username(UserContext.getUsername())
                .build();
        int insert = baseMapper.insert(groupDO);
        return insert > 0;
    }

    /**
     * 查询短链接分组
     *
     * @return 分组集合
     */
    @Override

    public List<GroupRespDTO> listGroup() {
        //TODO 从请求中获取用户名
        LambdaQueryWrapper<LinkGroupDO> queryWrapper = Wrappers.lambdaQuery(LinkGroupDO.class)
                .eq(LinkGroupDO::getDelFlag, 0)
                .eq(LinkGroupDO::getUsername, UserContext.getUsername())
                .orderByDesc(LinkGroupDO::getSortOrder, LinkGroupDO::getCreateTime);
        List<LinkGroupDO> linkGroups = baseMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(linkGroups, GroupRespDTO.class);
    }

    /**
     * 修改短链接分组
     *
     * @return 分组集合
     */
    @Override
    public Boolean updateGroup(GroupUpdateReqDTO groupSaveReqDTO) {
        LambdaUpdateWrapper<LinkGroupDO> queryWrapper = Wrappers.lambdaUpdate(LinkGroupDO.class)
                .eq(LinkGroupDO::getGid, groupSaveReqDTO.getGid())
                .eq(LinkGroupDO::getUsername, UserContext.getUsername())
                .set(LinkGroupDO::getName, groupSaveReqDTO.getName());
        LinkGroupDO linkGroupDO = new LinkGroupDO().setName(groupSaveReqDTO.getName());
        int update = baseMapper.update(linkGroupDO, queryWrapper);
        ThrowUtils.throwIf(update <= 0, LinkErrorEnums.LINK_GROUP_UPDATE_ERROR);
        return true;
    }

    @Override
    public Boolean deleteGroup(String gid) {
        LambdaUpdateWrapper<LinkGroupDO> queryWrapper = Wrappers.lambdaUpdate(LinkGroupDO.class)
                .eq(LinkGroupDO::getGid, gid)
                .eq(LinkGroupDO::getUsername, UserContext.getUsername())
                .set(LinkGroupDO::getDelFlag, 0);
        LinkGroupDO linkGroupDO = new LinkGroupDO().setDelFlag(1);
        int delete = baseMapper.update(linkGroupDO, queryWrapper);
        ThrowUtils.throwIf(delete <= 0, LinkErrorEnums.LINK_GROUP_UPDATE_ERROR);
        return true;
    }

    @Override
    public boolean groupSort(List<GroupSortReqDTO> groupSortReqs) {
        groupSortReqs.forEach(each -> {
            LinkGroupDO groupDO = LinkGroupDO.builder()
                    .gid(each.getGid())
                    .sortOrder(each.getSortOrder())
                    .build();
            LambdaUpdateWrapper<LinkGroupDO> updateWrapper = Wrappers.lambdaUpdate(LinkGroupDO.class)
                    .set(LinkGroupDO::getSortOrder, each.getSortOrder())
                    .eq(LinkGroupDO::getGid, each.getGid())
                    .eq(LinkGroupDO::getDelFlag, 0);
            update(groupDO, updateWrapper);
        });
        return true;
    }

    private boolean hasGid(String gid) {
        LambdaQueryWrapper<LinkGroupDO> queryWrapper = Wrappers.lambdaQuery(LinkGroupDO.class)
                .eq(LinkGroupDO::getGid, gid)
                //TODD 设置用户名
                .eq(LinkGroupDO::getUsername, UserContext.getUsername());
        LinkGroupDO hasGroup = baseMapper.selectOne(queryWrapper);
        return hasGroup == null;
    }
}




